/*******************************************************************************
 * Venice Noise Android Application
 * Copyright (C) 2011  Worcester Polytechnic Institute, Wesley Ripley
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; version 2
 * of the License
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * If you plan to use or modify any of this code please contact Fabio Carrera (carrera.fabio@gmail.com)
 * Also please attribute any code used in the final product to the developers. 
 * 
 * Author: Wesley Ripley (wripley@wpi.edu) 
 *******************************************************************************/
package org.venicenoise.app;

import android.app.ListActivity;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.Window;
import android.widget.Adapter;

/**
 * The activity that displays the SubmissionQue to the user
 * @author Wesley Ripley
 * @version 12/15/2011
 */
public class SubmissionQueActivity extends ListActivity{
	
	private SubmissionQue submissionQue;
	//whether or not to show thumbnails of photos or not
	private boolean showThumbnails = true;
			
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.submission_que);
        AppUtil.setVersionInHeader(this);
        
        submissionQue = new SubmissionQue(this);
    }
    
    /**Called when the activity is becoming visible to the user.**/
    @Override
    public void onStart()
    {
    	super.onStart();
    	SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(getBaseContext());
    	//get show thumbnail preference value
    	showThumbnails = prefs.getBoolean(getString(R.string.pref_showThumbKey), true);
    	//fill the list from the queue
    	fillData();
    }
    
    /** The final call you receive before your activity is destroyed */
	@Override
	public void onDestroy()
	{
		super.onDestroy();
		submissionQue.close();
		submissionQue = null;
	}
    
	/**
	 * Fill the list view with data
	 */
    private void fillData() {
        Cursor cursor = submissionQue.getCursorOverAll();
        startManagingCursor(cursor);

        setListAdapter(new NoiseDataListAdapter(this,cursor,false,showThumbnails));
        setSubmitButtonVisibility();
    }
    
    /**
     * Called when submit all button is clicked
     * (Set as onClick for submit all button)
     * @param view
     * 		The view that was clicked
     */
	public void submitAllClicked(View view)
	{
		final SubmitData submitter = new SubmitData(this,new SubmitData.PostExecutable() {
			public void onPostExecute() {
				afterSubmit();
			}
		});
		//submit the queue in the background
		submitter.execute(submissionQue);
	}

	/**
	 * Called after submitting has finished.
	 */
	public void afterSubmit() {
		fillData();
	}
	
	/**
	 * Set the visibility of the submit all button.
	 * If list has something in it, this makes the button visible.
	 * If the list is empty, this makes the button gone
	 */
    private void setSubmitButtonVisibility()
    {
    	View submitAllButton = findViewById(R.id.submitAllButton);
    	Adapter listAdapter = getListView().getAdapter();
    	if(listAdapter == null || listAdapter.isEmpty())
    	{
    		submitAllButton.setVisibility(View.GONE);
    	}
    	else
    	{
    		submitAllButton.setVisibility(View.VISIBLE);
    	}
    }

}
